<?php

/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program.  If not, see 
 * <http://www.gnu.org/licenses/>.
*/


// no direct access
defined('_JEXEC') or die('Restricted access');


$this->days_in_week_count     = count($this->days_in_week);

$available_text = JText::_('Available');
$today          = strtotime(date('Y-m-d'));

$uri =& JFactory::getURI();

?>
<div id="mod_appointments">
<script type="text/javascript">

$(function() {
	var $appointmentDetails = $('#appointment-details')
<?php if (!$this->has_errors) { ?>
		.hide()
<?php } ?>
		;
	$('.available').click(function() {
		$appointmentDetails.slideDown('slow');
		var offsetTop = $appointmentDetails.offset().top;
		$('html,body').animate({scrollTop: offsetTop}, 800);
	});
	$('#appointment-details-cancel').click(function() {
		$appointmentDetails.slideUp('fast');
	});
	$('.schedule .available').click(function() {
		$('#appointment-start-dt').attr('value', this.title);
	});
});

</script>
<style type="text/css">
#mod_appointments td.available {
	background-color: #FFF;
	border: 1px solid #DDD;
	border-top-color: #EEE;
	border-left-color: #EEE;
	color: #999;
	text-align: center;
	cursor: pointer;
}
#mod_appointments td.assigned {
	background-color: #D7FFD7;
	border: 2px solid #66CC33;
	border-top-color: #89D863;
	border-left-color: #89D863;
	text-align: center;
}
#mod_appointments td.taken {
	background-color: #F9F9F9;
}
#appointment-attending {
	font-size: 18px;
}
#mod_appointments label {
	display: block;
	float: left;
	width: 33%;
}
</style>

<form action="<?php echo $this->action ?>" method="post">
	<input name="week" type="hidden" value="<?php echo $this->week ?>"/>

	<p>
		<label for="appointment-attending"><?php echo JText::_('Attending') ?>:</label>
		<select id="appointment-attending" name="attending_id" onchange="this.form.submit()">
			<?php foreach ($this->attending_list as $att) {
				$selected = ($att->id == $this->attending->id ? ' selected="selected"' : '');
				?>
				<option value="<?php echo $att->id ?>"<?php echo $selected ?>><?php echo $att->firstname, ' ', $att->lastname ?></option>
			<?php } ?>
		</select>
	</p>
</form>

<form action="<?php echo $this->action ?>" method="post">
	<input name="task" type="hidden" value="requestAppointment"/>
	<input name="week" type="hidden" value="<?php echo $this->week ?>"/>
	<input name="attending_id" type="hidden" value="<?php echo $this->attending->id ?>"/>

	<fieldset id="appointment-details">
		<legend><?php echo JText::_('Appointment Details'); ?></legend>

		<?php if ($this->has_errors) { ?>
			<ul class="errors">
				<?php foreach ($this->errors as $error) { ?>
					<li><?php echo $error ?></li>
				<?php } ?>
			</ul>
		<?php } ?>

		<p>
			<label for="appointment-name"><?php echo JText::_('Your Name'); ?>:</label>
			<input id="appointment-name" name="name" value="<?php echo $this->form->name ?>" style="width:300px"/>
		</p>
		<p>
			<label for="appointment-email"><?php echo JText::_('E-mail'); ?>:</label>
			<input id="appointment-email" name="email" value="<?php echo $this->form->email ?>" style="width:300px"/>
		</p>
		<p>
			<label for="appointment-start-dt"><?php echo JText::_('Date and time'); ?>:</label>
			<input id="appointment-start-dt" name="start_dt" value="<?php echo $this->form->start_dt ?>" style="width:300px"/>
		</p>
		<p>
			<label for="appointment-telephone"><?php echo JText::_('Telephone'); ?>:</label>
			<input id="appointment-telephone" name="telephone" value="<?php echo $this->form->telephone ?>" style="width:300px"/>
		</p>
		<p>
			<label for="appointment-cellphone"><?php echo JText::_('Cellphone'); ?>:</label>
			<input id="appointment-cellphone" name="cellphone" value="<?php echo $this->form->cellphone ?>" style="width:300px"/>
		</p>
		<p>
			<label for="appointment-address"><?php echo JText::_('Address'); ?>:</label>
			<input id="appointment-address" name="address" value="<?php echo $this->form->address ?>" style="width:300px"/>
		</p>
		<p>
			<label for="appointment-treatment"><?php echo JText::_('Treatment'); ?>:</label>
			<select id="appointment-treatment" name="treatment_id" style="width:300px">
				<?php foreach ($this->treatment_list as $treatment) { ?>
					<option value="<?php echo $treatment->id ?>"<?php echo $treatment->id == $this->form->treatment_id ? ' selected' : '' ?>><?php echo $treatment->name ?></option>
				<?php } ?>
			</select>
		</p>
		<p>
			<label for="appointment-comments"><?php echo JText::_('Comments'); ?>:</label>
			<textarea id="appointment-comments" name="appointment-comments" rows="8" cols="45" style="width:300px"><?php echo $this->form->comments ?></textarea>
		</p>
		<hr />
		<p style="text-align:center">
			<button type="submit"><?php echo JText::_('Request') ?></button>
			<button type="button" id="appointment-details-cancel"><?php echo JText::_('Cancel') ?></button>
		</p>
	</fieldset>

	<p style="text-align:right"><a href="<?php echo $this->prev_week_link ?>"><?php echo JText::_('<< Prev Week') ?></a> |
		<a href="<?php echo $this->next_week_link ?>"><?php echo JText::_('Next Week >>') ?></a></p>

	<table class="schedule">
		<thead>
			<tr>
				<th>&nbsp;</th>
				<?php foreach ($this->days_in_week as $day_in_week) { ?>
					<th><?php echo JHTML::_('date', $day_in_week, JText::_('%a %d/%b')) ?></th>
				<?php } ?>
			</tr>
		</thead>

		<?php
		foreach ($this->available_hours as $range) {
			for ($minute = $range->start_minute; $minute < $range->end_minute; $minute += $this->attending->time_span_minutes) {
		?>
				<tr>
					<th><?php echo $this->formatHour($minute), ' ', $minute ?></th>
					<?php
					for ($day_of_week = 0; $day_of_week < $this->days_in_week_count; ++$day_of_week) {
						$start_dt = date('Y-m-d H:i:s', $this->days_in_week[$day_of_week] + ($minute * 60));
						if (isset($this->appointments_this_week[$day_of_week][$minute])) {
							$appointment = $this->appointments_this_week[$day_of_week][$minute];
							if ($minute == $appointment->start_minute) {
								$row_span = $appointment->row_span > 1 ? " rowspan=\"$appointment->row_span\"" : '';
								if (isset($appointment->name)) {
								?>
									<td class="assigned"<?php echo $row_span ?>>
										<?php echo $appointment->name ?><br/>
										<?php echo date('h:i a', $appointment->start_uts), ' - ', date('h:i a', $appointment->end_uts) ?>
									</td>
								<?php } else { ?>
									<td class="taken">&nbsp;</td>
								<?php
								}
							}
						} else {
							if ($this->days_in_week[$day_of_week] > $today) {
							?>
								<td class="available" title="<?php echo $start_dt ?>"><?php echo $available_text ?></td>
							<?php
							} else {
							?>
								<td>&nbsp;</td>
							<?php
							}
						}
					}
					?>
				</tr>
			<?php } ?>
			<tr>
				<td class="separator" colspan="<?php echo $this->days_in_week_count ?>"> </td>
			</tr>
		<?php } ?>
	</table>
</form>

</div>
